package com.itheima.mq.rabbitmq.configuration;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;

/**
 * 测试Direct类型的交换机
 */
// @Component
public class RabbitmqDirectExchangeConfiguration {
    
    // 声明交换机
    @Bean(name = "direct.exchange")
    public Exchange directExchange() {
        return ExchangeBuilder.directExchange("direct.exchange").durable(true).build();
    }
    
    // 声明队列
    @Bean(name = "direct.queue_01")
    public Queue commonQueue01() {
        return QueueBuilder.durable("direct.queue_01").build();
    }
    
    // 声明队列
    @Bean(name = "direct.queue_02")
    public Queue commonQueue02() {
        return QueueBuilder.durable("direct.queue_02").build();
    }
    
    // 完成队列和交换机的绑定
    @Bean
    public Binding directQueueBindToFanoutQueue01(
            @Qualifier(value = "direct.exchange") Exchange exchange,
            @Qualifier(value = "direct.queue_01") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("create").noargs();
    }
    
    // 完成队列和交换机的绑定
    @Bean
    public Binding directQueueBindToFanoutQueue02(
            @Qualifier(value = "direct.exchange") Exchange exchange,
            @Qualifier(value = "direct.queue_02") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("delete").noargs();
    }
    
    //--------------惰性队列-------------------
    
    // 声明队列
    @Bean(name = "direct.queue_13")
    public Queue commonQueue13() {
        QueueBuilder durable = QueueBuilder.durable("direct.queue_13");
        //设置惰性队列,如果是惰性,就会持久化到存储,在内存中不驻留
        durable.lazy();
        // durable.ttl(30000);
        return durable.build();
    }
    
    @Bean
    public Binding directQueueBindToFanoutQueue13(
            @Qualifier(value = "direct.exchange") Exchange exchange,
            @Qualifier(value = "direct.queue_13") Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("lazy").noargs();
    }
    
}
